﻿//---------------------------------------------------------------------------
// 
// File: HtmlTokenType.cs
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
// Description: Definition of token types supported by HtmlLexicalAnalyzer
//
//---------------------------------------------------------------------------

namespace HTMLConverter.XamlConverters
{
    using System.Xml.Linq;
    // DependencyProperty
    // TextElement
    using HTMLConverter.Xml;

    /// <summary>
    /// HtmlToXamlConverter is a static class that takes an HTML string
    /// and converts it into XAML
    /// </summary>
    internal class TitleConverter : IConverter
    {
        public Tag Type
        {
            get { return Tag.Title; }
        }

        public XmlElement Convert(XNode htmlNode)
        {
            XmlElement span = new XmlElement(HtmlConverter.Xaml_Span, HtmlConverter.Xaml_Namespace);
            if (htmlNode is XmlElement)
            {
                XmlElement htmlElement = htmlNode as XmlElement;
                string tagname = htmlElement.LowerName;
                for (XNode htmlChildNode = htmlElement.FirstNode; htmlChildNode != null; htmlChildNode = htmlChildNode.NextNode)
                {
                    if (htmlChildNode is XmlText)
                    {
                        XmlText htmlChildText = htmlChildNode as XmlText;
                        if (string.IsNullOrEmpty(htmlChildText.Value.Trim()) == false)
                        {
                            XmlElement bold = Converters.Collection[Tag.Bold].Convert(htmlChildNode);
                            span.AppendChild(ApplyFontSize(tagname, bold));
                        }
                    }
                }
            }

            if (span.FirstNode != null)
            {
                return span;
            }

            return null;
        }

        static XmlElement ApplyFontSize(string tag, XmlElement bold)
        {
            if (bold != null)
            {
                switch (tag)
                {
                    case "h1":
                        bold.SetAttribute(HtmlConverter.Xaml_FontSize, HtmlConverter.Xaml_FontSize_XXLarge);
                        break;
                    case "h2":
                        bold.SetAttribute(HtmlConverter.Xaml_FontSize, HtmlConverter.Xaml_FontSize_XLarge);
                        break;
                    case "h3":
                        bold.SetAttribute(HtmlConverter.Xaml_FontSize, HtmlConverter.Xaml_FontSize_Large);
                        break;
                    case "h4":
                        bold.SetAttribute(HtmlConverter.Xaml_FontSize, HtmlConverter.Xaml_FontSize_Medium);
                        break;
                    case "h5":
                        bold.SetAttribute(HtmlConverter.Xaml_FontSize, HtmlConverter.Xaml_FontSize_Small);
                        break;
                    case "h6":
                        bold.SetAttribute(HtmlConverter.Xaml_FontSize, HtmlConverter.Xaml_FontSize_XSmall);
                        break;
                }

                return bold;
            }

            return null;
        }
    }
}
